<template>
  <div class="rightBodyContent">
    <div class="rightBodyContent-in">
      <div class="search-line">
        <div class="search-list-line">
          <div class="search-list">
            <span class="search-list-title">班级：</span>
            <div class="search-list-content">
              <component
                  v-bind:is="item.propertyType"
                  :property.sync="item"
                  v-for="(item, index) in getPower"
                  :key="index"
                  @updateVal="updateVal"
              ></component>
            </div>
          </div>
          <div class="search-list">
            <span class="search-list-title">学生姓名：</span>
            <div class="search-list-content">
              <el-input v-model="searWord.student_name" placeholder="学生姓名" size="small" class="w-140 mr-10"></el-input>
            </div>
          </div>
        </div>
        <div class="search-list">
          <el-button type="primary" @click.native="searchBtn('search')" size="small"><i class="el-icon-search"></i>搜索</el-button>
          <el-button @click.native="searchBtn('refresh')" size="small"><i class="el-icon-refresh"></i>重置刷新</el-button>
        </div>
      </div>
      <MaterialInteraction :arryList="arryList" :tableoption="tableoption" @downs="downs" @getCornerFn="getCornerFn" @showMaterials="showMaterials" />
      <!--工具条 分页-->
      <el-col :span="24" class="toolbar">
        <el-pagination
            background
            :page-size="page.pagesize"
            :current-page="page.pageindex"
            layout="total, prev, pager, next"
            :total="page.total"
            @current-change="handlePageChange"></el-pagination>
      </el-col>
      <activeVideoDialog :videosrcCheck="videosrcCheck" :visible.sync="dialogvideoVisible"></activeVideoDialog>
      <!-- 材料展示弹窗 -->
      <el-dialog title="材料展示" :visible.sync="materialDialogVisible" width="80%">
        <div class="material-container">
          <div class="material-item" v-for="(material, index) in selectedMaterials" :key="index">
            <div class="material-image">
              <img v-if="material.pic" :src="material.pic" alt="Material" style="max-width: 100%; height: auto;">
            </div>
            <div class="material-name">{{ material.name }}</div>
          </div>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import {pagination, tableoption} from '@/utils/util.js'
import studentCollection from "./commentCollection/studentCollection";
import teachCollection from "./commentCollection/teachCollection";
import comment from "./commentCollection/comment"
import evaluating from "./commentCollection/evaluating";
import {cornerDetail} from '@/api/admin/corner.js'
import {collectionIndex} from '@/api/admin/collection'
import MaterialInteraction from "@/views/admin/Corner/commentCollection/MaterialInteraction.vue";

export default {
  components: {
    MaterialInteraction,
    studentCollection,
    teachCollection,
    comment,
    evaluating
  },
  data() {
    return {
      arryList: [],
      valueText: [],
      cornerlist: [],
      pickerOptions1: {
        disabledDate(time) {
          return time.getTime() > Date.now(); //  - 8.64e7
        }
      },
      selectTipslist: [],
      radio: 0,
      tableoption: tableoption,
      page: {
        pageindex: pagination.pageIndex,
        pagesize: pagination.pageSize,
        total: pagination.total
      },
      // 搜索
      searWord: {
        corner_id: '',
        tag_ids: '',
        start_time: '',
        end_time: '',
        class_id: '',
        section_id: '',
        student_name: ''
      },
      videosrcCheck: '',
      dialogvideoVisible: false,
      tabType: 2,
      tabLine: 0,
      getPower: {
        'section_id': {
          name: 'section_id', // key名称
          default: '', // 默认值
          propertyType: "Select", // 组件名称
          placeholder: '年段', // 默认值
          valueText: '', // 值
          leve: 2,
          api: this.api.user_sectionindex, // 下拉框的请求接口
          keyName: 'section_name',// 调用的字段
          child: [
            {
              name: 'class_id',
              default: '',
              propertyType: "",
              placeholder: '班级',
              valueText: '',
              api: this.api.user_classesindex,
              keyName: 'class_name',
            }
          ]
        }
      },
      materialDialogVisible: false,
      selectedMaterials: []
    }
  },
  created() {
    this.InitFn();
    this.getTagsFn();
    this.cornerAllFn();
  },
  methods: {
    // 初始化
    async InitFn() {
      let optin = JSON.parse(JSON.stringify(this.searWord));
      optin.p = this.page.pageindex;
      optin.limit = this.page.pagesize;
      let url = this.api.corner.studentCornerMaterialIntent; // 确认这个地址是否正确
      if (!url) {
        return;
      }
      try {
        let response = await this.$fetch(url, optin);
        console.log('接口返回的数据：', response);
        if (response.code == "0") { // 假设接口返回的数据中有一个code字段表示请求成功
          this.arryList = response.data; // 假设接口返回的数据中有一个data字段包含列表数据
          this.page.total = response.total; // 更新总记录数
        } else {
          console.error('请求失败:', response.message);
        }
      } catch (error) {
        console.error('请求错误:', error);
      }
    },
    // 分页
    handlePageChange(val) {
      this.page.pageindex = val;
      this.InitFn();
    },
    // 搜索
    searchBtn(option) {
      this.page.pageindex = 1;
      if (option === 'refresh') {
        this.searWord.corner_id = '';
        this.searWord.student_name = '';
        this.searWord.tag_ids = '';
        this.searWord.start_time = '';
        this.searWord.end_time = '';
        this.valueText = [];
        this.radio = 0;
        this.searWord.class_id = '';
        this.searWord.section_id = '';
        let newGetPower = JSON.parse(JSON.stringify(this.getPower));
        newGetPower['section_id'].valueText = '';
        newGetPower['section_id'].child[0].valueText = '';
        this.getPower = newGetPower;
      } else {
        if (this.valueText && this.valueText.length) {
          this.searWord.start_time = this.valueText[0];
          this.searWord.end_time = this.valueText[1];
        } else {
          this.searWord.start_time = '';
          this.searWord.end_time = '';
        }
      }
      this.InitFn();
    },
    // 更新值
    updateVal(option) {
      if (option.input) {
        this.searWord[option.input.name] = option.input.valueText;
      } else if (option.select) {
        for (let i in option.select) {
          if (option.select[i].name) {
            this.searWord[option.select[i].name] = option.select[i].valueText;
          } else {
            this.searWord[option.select[i - 1].name] = option.select[i].valueText ? option.select[i].valueText : option.select[i - 1].valueText;
          }
        }
      }
    },
    // 下载图片
    downs(imgsrc) {
      let alink = document.createElement("a");
      alink.download = "123"; // 图片名
      alink.href = imgsrc;
      alink.click();
    },
    // 获取区角详情
    getCornerFn(option) {
      console.log('请求区角详情', option);
      cornerDetail({id: option.row.corner_id}).then(res => {
        let list = this.arryList[option.index];
        let a = JSON.parse(list.list);
        list.showDetail = true;
        let assess = {};
        for (let item of res.vo.assess) {
          assess[item.items_id] = JSON.parse(JSON.stringify(item));
        }
        for (let item of a) {
          if (assess[item.items_id]) {
            let b = assess[item.items_id];
            if (b.fraction) {
              for (let item_fraction of b.fraction) {
                if (Number(item_fraction.id) === Number(item.evaluate_id)) {
                  item.desc = item_fraction.desc;
                }
              }
            }
          }
        }
        list.list = JSON.stringify(a);
        this.$set(this.arryList, option.index, list);
      });
    },
    // 获取标签
    async getTagsFn() {
      let data = await this.$fetch(this.api.corner.comment.tagsIndex, {});
      console.log('dataloi:', data);
      this.selectTipslist = data.list.data;
    },
    // 获取所有区角
    async cornerAllFn() {
      let data = await this.$fetch(this.api.admin_corner_index, {p: 0});
      this.cornerlist = data.list;
    },
    // 显示视频
    showVideoSrc(videoSrc) {
      this.videosrcCheck = videoSrc;
      this.dialogvideoVisible = true;
    },
    // 切换标签
    handleClick() {
      if (Number(this.tabLine) !== Number(this.tabType)) {
        this.tabLine = this.tabType;
        this.page.pageindex = 1;
        this.arryList = [];
        this.searWord.corner_id = '';
        this.searWord.student_name = '';
        this.searWord.tag_ids = '';
        this.searWord.start_time = '';
        this.searWord.end_time = '';
        this.valueText = [];
        this.radio = 0;
        this.searWord.class_id = '';
        this.searWord.section_id = '';
        let newGetPower = JSON.parse(JSON.stringify(this.getPower));
        newGetPower['section_id'].valueText = '';
        newGetPower['section_id'].child[0].valueText = '';
        this.getPower = newGetPower;
        this.InitFn();
      }
    },
    // 展示材料
    showMaterials(materials) {
      this.selectedMaterials = materials;
      this.materialDialogVisible = true;
    }
  }
}
</script>

<style scoped>
.search-line {
  border: 1px solid #ddd;
  border-radius: 5px;
  padding: 0 10px;
  margin-bottom: 10px;
}

.search-list {
  height: 50px;
  display: flex;
  align-items: center;
  padding: 0 10px;
}

.search-list:last-child {
  border-bottom: none;
}

div.v-dropdown-caller.v-dropdown-caller--full-width {
  display: block !important;
}

.search-list-line {
  display: flex;
  border-bottom: 1px solid #f5f5f5;
}

/* 材料展示弹窗样式 */
.material-container {
  display: flex;
  flex-wrap: wrap;
  gap: 20px;
  justify-content: flex-start;
}

.material-item {
  border: 1px solid #ddd;
  border-radius: 5px;
  padding: 10px;
  width: 150px;
  text-align: center;
}

.material-image {
  margin-bottom: 10px;
}

.material-name {
  font-weight: bold;
}
</style>
